<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="1000px">
    <el-table
      v-loading="loading"
      :data="recordList"
      :stripe="true"
      :show-overflow-tooltip="true"
    >
      <el-table-column label="订单号" align="center" prop="orderNumber" min-width="180" />
      <el-table-column label="金额" align="center" prop="amount">
        <template #default="scope">
          {{ formatAmount(scope.row.amount) }}
        </template>
      </el-table-column>
      <el-table-column label="交易渠道" align="center" prop="transactionChannel">
        <template #default="scope">
          {{ formatTransactionChannel(scope.row.transactionChannel) }}
        </template>
      </el-table-column>
      <el-table-column label="业务类型" align="center" prop="businessType" />
      <el-table-column label="变动后余额" align="center" prop="balanceAfter">
        <template #default="scope">
          {{ formatAmount(scope.row.balanceAfter) }}
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" min-width="150" />
      <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter" min-width="150" />
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getRecordList"
    />
    <template #footer>
      <el-button @click="dialogVisible = false">关 闭</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import {FinancialFlowApi} from '@/api/javainfo/financialflow'
import {dateFormatter} from '@/utils/formatTime'

/** 用户充值记录 弹窗 */
defineOptions({ name: 'UserRechargeRecordDialog' })

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('用户充值记录') // 弹窗的标题
const loading = ref(false) // 表格的加载中
const recordList = ref([]) // 充值记录列表数据
const total = ref(0) // 总记录数
const userId = ref<number>() // 用户ID

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  userId: undefined,
  amountType: 'RECHARGE', // 充值类型
})

/** 打开弹窗 */
const open = async (id: number) => {
  dialogVisible.value = true
  userId.value = id
  queryParams.userId = id
  await getRecordList()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 获取用户充值记录列表 */
const getRecordList = async () => {
  loading.value = true
  try {
    // 调用财务流水API，根据用户ID和金额类型查询充值记录
    const data = await FinancialFlowApi.getFinancialFlowPage(queryParams)
    recordList.value = data.list || []
    total.value = data.total || 0
  } finally {
    loading.value = false
  }
}

/** 格式化金额（分转元） */
const formatAmount = (amount) => {
  if (amount === undefined || amount === null) return '0.00'
  return (amount / 100).toFixed(2)
}

/** 格式化交易渠道 */
const formatTransactionChannel = (channel) => {
  const channelMap = {
    'ALIPAY': '支付宝',
    'WECHAT': '微信',
    'BANK': '网商银行',
    'BALANCE': '现金',
    'SYSTEM': '系统'
  }
  return channelMap[channel] || channel
}
</script>

